<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-header">
        <div class="logo">
          <img src="@/assets/logo.png" alt="汽车租赁平台">
          <h1>汽车租赁平台</h1>
        </div>
      </div>
      
      <el-tabs v-model="activeTab" class="register-tabs">
        <el-tab-pane label="普通注册" name="normal">
          <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
            <el-form-item prop="username">
              <el-input
                v-model="registerForm.username"
                prefix-icon="el-icon-user"
                placeholder="用户名"
                clearable
              ></el-input>
            </el-form-item>
            
            <el-form-item prop="phone">
              <el-input
                v-model="registerForm.phone"
                prefix-icon="el-icon-mobile-phone"
                placeholder="手机号"
                clearable
              ></el-input>
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="registerForm.password"
                prefix-icon="el-icon-lock"
                placeholder="密码"
                show-password
                clearable
              ></el-input>
            </el-form-item>
            
            <el-form-item prop="confirmPassword">
              <el-input
                v-model="registerForm.confirmPassword"
                prefix-icon="el-icon-lock"
                placeholder="确认密码"
                show-password
                clearable
              ></el-input>
            </el-form-item>
            
            <el-form-item>
              <el-button type="primary" :loading="loading" @click="handleRegister" class="submit-btn">注册</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        
        <el-tab-pane label="短信验证码注册" name="sms">
          <el-form ref="smsRegisterForm" :model="smsRegisterForm" :rules="smsRegisterRules" class="register-form">
            <el-form-item prop="username">
              <el-input
                v-model="smsRegisterForm.username"
                prefix-icon="el-icon-user"
                placeholder="用户名"
                clearable
              ></el-input>
            </el-form-item>
            
            <el-form-item prop="phone">
              <el-input
                v-model="smsRegisterForm.phone"
                prefix-icon="el-icon-mobile-phone"
                placeholder="手机号"
                clearable
              ></el-input>
            </el-form-item>
            
            <el-form-item prop="code">
              <div class="code-input-group">
                <el-input
                  v-model="smsRegisterForm.code"
                  prefix-icon="el-icon-message"
                  placeholder="验证码"
                  clearable
                ></el-input>
                <el-button 
                  type="primary" 
                  :disabled="isSending" 
                  @click="sendCode" 
                  class="send-code-btn"
                >
                  {{ sendBtnText }}
                </el-button>
              </div>
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="smsRegisterForm.password"
                prefix-icon="el-icon-lock"
                placeholder="密码"
                show-password
                clearable
              ></el-input>
            </el-form-item>
            
            <el-form-item>
              <el-button type="primary" :loading="loading" @click="handleSmsRegister" class="submit-btn">注册</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      
      <div class="form-footer">
        <p>已有账号? <el-button type="text" @click="goToLogin">立即登录</el-button></p>
      </div>
    </div>
    
    <div class="background-image"></div>
  </div>
</template>

<script>
import { register, registerBySms, sendSmsCode } from '@/api/user'

export default {
  name: 'Register',
  data() {
    // 验证两次密码是否一致
    const validatePass = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    
    // 手机号验证规则
    const validatePhone = (rule, value, callback) => {
      const phoneReg = /^1[3-9]\d{9}$/
      if (!value) {
        callback(new Error('请输入手机号'))
      } else if (!phoneReg.test(value)) {
        callback(new Error('请输入正确的手机号码'))
      } else {
        callback()
      }
    }
    
    return {
      activeTab: 'normal',
      registerForm: {
        username: '',
        phone: '',
        password: '',
        confirmPassword: ''
      },
      smsRegisterForm: {
        username: '',
        phone: '',
        code: '',
        password: ''
      },
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少为6个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ]
      },
      smsRegisterRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { len: 6, message: '验证码长度为6位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少为6个字符', trigger: 'blur' }
        ]
      },
      loading: false,
      isSending: false,
      countdown: 60,
      sendBtnText: '获取验证码',
      timer: null
    }
  },
  methods: {
    // 普通注册
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          
          const registerData = {
            username: this.registerForm.username,
            phone: this.registerForm.phone,
            password: this.registerForm.password
          }
          
          register(registerData)
            .then(() => {
              this.$message.success('注册成功，请登录')
              this.$router.push('/login')
            })
            .catch(error => {
              this.$message.error(error.message || '注册失败，请稍后重试')
            })
            .finally(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    
    // 短信验证码注册
    handleSmsRegister() {
      this.$refs.smsRegisterForm.validate(valid => {
        if (valid) {
          this.loading = true
          
          const registerData = {
            username: this.smsRegisterForm.username,
            phone: this.smsRegisterForm.phone,
            code: this.smsRegisterForm.code,
            password: this.smsRegisterForm.password
          }
          
          registerBySms(registerData)
            .then(() => {
              this.$message.success('注册成功，请登录')
              this.$router.push('/login')
            })
            .catch(error => {
              this.$message.error(error.message || '注册失败，请稍后重试')
            })
            .finally(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    
    // 发送验证码
    sendCode() {
      this.$refs.smsRegisterForm.validateField('phone', (errorMsg) => {
        if (!errorMsg) {
          this.isSending = true
          this.countdown = 60
          this.sendBtnText = `${this.countdown}秒后重发`
          
          sendSmsCode(this.smsRegisterForm.phone)
            .then(response => {
              this.$message.success('验证码发送成功，请注意查收')
              // 演示/测试环境自动填充验证码
              if (response && response.code) {
                this.smsRegisterForm.code = response.code
              }
              
              this.timer = setInterval(() => {
                if (this.countdown > 0) {
                  this.countdown--
                  this.sendBtnText = `${this.countdown}秒后重发`
                } else {
                  clearInterval(this.timer)
                  this.isSending = false
                  this.sendBtnText = '获取验证码'
                }
              }, 1000)
            })
            .catch(error => {
              this.isSending = false
              this.sendBtnText = '获取验证码'
              this.$message.error(error.message || '验证码发送失败，请稍后重试')
            })
        }
      })
    },
    
    goToLogin() {
      this.$router.push('/login')
    },
    
    // 组件销毁前清除定时器
    beforeDestroy() {
      if (this.timer) {
        clearInterval(this.timer)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  
  .background-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url('https://img.zcool.cn/community/0190a45af3ef2ea801214a3ef43446.jpg@1280w_1l_2o_100sh.jpg') no-repeat center center;
    background-size: cover;
    filter: brightness(0.7);
    z-index: -1;
  }
  
  .register-box {
    width: 400px;
    background-color: rgba(255, 255, 255, 0.95);
    border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
    padding: 30px;
    position: relative;
    z-index: 1;
    
    .register-header {
      margin-bottom: 20px;
      text-align: center;
      
      .logo {
        display: flex;
        align-items: center;
        justify-content: center;
        
        img {
          height: 40px;
          margin-right: 10px;
        }
        
        h1 {
          font-size: 22px;
          margin: 0;
          color: #333;
        }
      }
    }
    
    .register-tabs {
      margin-bottom: 15px;
    }
    
    .register-form {
      .code-input-group {
        display: flex;
        
        .el-input {
          flex: 1;
        }
        
        .send-code-btn {
          width: 120px;
          margin-left: 10px;
          font-size: 12px;
        }
      }
      
      .submit-btn {
        width: 100%;
      }
      
      .form-footer {
        text-align: center;
        margin-top: 15px;
        color: #666;
      }
    }
  }
}

@media (max-width: 576px) {
  .register-container {
    .register-box {
      width: 90%;
      padding: 20px;
    }
  }
}
</style> 